package com.yejing.redisdemoTest;

import lombok.extern.slf4j.Slf4j;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.DefaultJedisClientConfig;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;

import java.util.List;
import java.util.Map;

@Slf4j
public class HashDemo {
    public Jedis jedis = null;

    @Before
    public void init() {
        if(null == jedis){
            DefaultJedisClientConfig config =
                    DefaultJedisClientConfig.builder() //
                            .password("123456") //
                            .build(); //
            HostAndPort hostPort = new HostAndPort("43.136.67.62", 6379);
            jedis = new Jedis(hostPort, config);
        }
    }


    @Test
    public void testHSet(){
//        long l = jedis.hset("user:001","id","001");
//        log.info(l+"");
//        l = jedis.hset("user:001","name","yejing");
//        log.info(l+"");
//        l = jedis.hset("user:001","age","34");
//        log.info(l+"");
//        l = jedis.hset("user:001","addr","guangzhou");
//        log.info(l+"");
//        l = jedis.hset("user:001","hobby","pograme");
//        log.info(l+"");


//        long l = jedis.hset("user:002","id","002");
//        log.info(l+"");
//        l = jedis.hset("user:002","name","zhangsan");
//        log.info(l+"");
//        l = jedis.hset("user:002","age","35");
//        log.info(l+"");
//        l = jedis.hset("user:002","addr","shanghai");
//        log.info(l+"");
//        l = jedis.hset("user:002","hobby","footbar");
//        log.info(l+"");

        long l = jedis.hset("project:001","id","001");
        log.info(l+"");
        l = jedis.hset("project:001","name","空压震动噪音");
        log.info(l+"");
    }

    @Test
    public void testHGet(){

        String name = jedis.hget("user:001", "name");
        log.info(name);

    }

    @Test
    public void testExists(){
        boolean b = jedis.hexists("user:001", "name");
        log.info(b+"");
    }

    @Test
    public void testValues(){
        List<String> hvals = jedis.hvals("user:001");
        hvals.stream().forEach(log::info);
    }


    @Test
    public void testHLen(){
        long hlen = jedis.hlen("user:001");
        log.info(hlen+"");
    }


    @Test
    public void testHgetAll(){
        Map<String, String> all = jedis.hgetAll("user:001");
        for (Map.Entry<String, String> entry : all.entrySet()) {
            log.info("{}:{}",entry.getKey(),entry.getValue());
        }
    }

    @After
    public void end() {
        jedis.close();
    }

}
